package utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.ConcurrentLinkedQueue;

class Creater implements Runnable {
    private ConcurrentLinkedQueue<String> queue;
    private String contents;

    public Creater() {
    }

    public Creater(ConcurrentLinkedQueue<String> queue, String contents) {
        this.queue = queue;
        this.contents = contents;
    }

    public void run() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
            queue.add(contents);

            class DealFile implements Runnable {
                private FileOutputStream out;
                private ConcurrentLinkedQueue<String> queue;

                public DealFile() {
                }

                public DealFile(FileOutputStream out, ConcurrentLinkedQueue<String> queue) {
                    this.out = out;
                    this.queue = queue;
                }

                public void run() {
                    while (true) {//循环监听
                        if (!queue.isEmpty()) {
                            try {
                                out.write(queue.poll().getBytes());
                            } catch (IOException e) {
                                e.printStackTrace();
                                try {
                                    Thread.sleep(100);
                                } catch (Exception ex) {
                                    ex.printStackTrace();
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
